﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizOfficialNews : CustBizObject
    {
        private CDmDjkOfficialNews m_CDmDjkOfficialNews = new CDmDjkOfficialNews();
        private CDmDjkOfficialNews oDmDjkOfficialNews
        {
            get { return m_CDmDjkOfficialNews; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizOfficialNews()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkOfficialNews.DJK_OFFICIAL_NEWS);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["OFFICIAL_NEWS_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 通知信息列表
        /// </summary>
        /// <returns>OFFICIAL_NEWS_ID：通知ID，OFFICIAL_NEWS_TITLE：通知标题</returns>
        public DataSet SelectNewsByIds()
        {
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT OFFICIAL_NEWS_ID,OFFICIAL_NEWS_TITLE FROM DJK_OFFICIAL_NEWS
    WHERE NEWS_IS_DELETED=0 AND NEWS_IS_DISPLAY=1
    ORDER BY NEWS_PUBLISH_TIME DESC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        /// <summary>
        /// 通知信息表详情
        /// </summary>
        /// <param name="newsId">通知编号</param>
        /// <returns>OFFICIAL_NEWS_ID：通知ID，OFFICIAL_NEWS_TITLE：通知标题,OFFICIAL_NEWS_CONTENT:通知内容，NEWS_PUBLISH_BY：发布者，NEWS_PUBLISH_TIME：通知发布时间，NEWS_EXPIRA_TIME：通知结束时间，NEWS_RMK：通知备注</returns>
        public DataSet GetNewsDetailsById(string newsId)
        {
            if (string.IsNullOrEmpty(newsId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("NEWSID", ParameterDirection.Input, EDbType.String, 36, newsId.Replace("'", "").Replace(",", "','")));
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT OFFICIAL_NEWS_ID,OFFICIAL_NEWS_TITLE,OFFICIAL_NEWS_CONTENT,
    NEWS_PUBLISH_BY,NEWS_PUBLISH_TIME,NEWS_EXPIRA_TIME,NEWS_RMK FROM DJK_OFFICIAL_NEWS
    WHERE NEWS_IS_DELETED=0 AND NEWS_IS_DISPLAY=1 AND OFFICIAL_NEWS_ID=@NEWSID");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }


        public DataSet GetOfficialNewsListBySP(string startTime, string stopTime, string keyValue, bool isDel, bool isDis)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();

            parmCollection.Add(new CDbParm("starttime", ParameterDirection.Input, EDbType.String, 100, startTime.Replace("'", "").Replace(",", "','")));
            parmCollection.Add(new CDbParm("stoptime", ParameterDirection.Input, EDbType.String, 100, stopTime.Replace("'", "").Replace(",", "','")));
            parmCollection.Add(new CDbParm("newskey", ParameterDirection.Input, EDbType.String, 100, keyValue.Replace("'", "").Replace(",", "','")));
            parmCollection.Add(new CDbParm("isdel", ParameterDirection.Input, EDbType.Boolean, 100, isDel));
            parmCollection.Add(new CDbParm("isdis", ParameterDirection.Input, EDbType.Boolean, 100, isDis));

            return this.DataObj.QueryBySP("DJK_SELECT_OFFICIAL_NEWS_FOR_VIEW", parmCollection);
        }
    }
}
